package com.web08.cookie;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLDecoder;

/**
 * @author wzm
 * @create 2023-11-13 15:30
 * project_name:web2310
 * @Description
 */

@WebServlet("/bServlet")
public class BServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //能不能获取cookie
        //获取客户端每次携带过来的cookie
        Cookie[] cookies = req.getCookies();
        //遍历   cookie存在多个值
        for (Cookie cookie : cookies) {
            String name = cookie.getName();//获取cookiekey
            if(name.equals("username")){
                String value = cookie.getValue();//%e5%bcA0B8%89

                //如果出现不能存储中文时取值要URL解码
            /*    value =   URLDecoder.decode(value,"UTF-8");
                System.out.println("解码后的数据：" + value);*/


                System.out.println("Cookie的值是：" + value);
                break;
            }

        }
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
       doPost(req, resp);
    }
}
